import java.util.Deque;
import java.util.LinkedList;

public class MyBinaryTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;
    public int TreeSize;

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        /*B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        D.left = H;*/
        root = A;
        return root;
    }

    public void size(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeSize++;
        size(root.left);
        size(root.right);
    }


    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    public int size2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftsize = size2(root.left);
        int rightsize = size2(root.right);
        return leftsize + rightsize + 1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */public static int leafSize = 0;

    public void getLeafNodeCount1(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    /*
     获取叶子节点的个数：子问题
     */
    public int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int leftLeaf = getLeafNodeCount2(root.left);
        int rightLeaf = getLeafNodeCount2(root.right);
        return leftLeaf + rightLeaf;
    }
    public boolean isCompleteTree(TreeNode root){
        if(root == null){
            return true;
        }
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur == null){
                break;
            }
            queue.offer(cur.left);
            queue.offer(cur.right);

        }
        while(!queue.isEmpty()){
            TreeNode tmp = queue.poll();
            if(tmp!=null){
                return false;
            }
        }
        return true;
    }
}
